package cn.iocoder.yudao.module.ai.service.operationequ;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.operationequ.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.operationequ.OperationEquDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.operationequ.OperationEquMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * 运营中心-算力设备 Service 实现类
 *
 * @author 侯俊青
 */
@Service
@Validated
public class OperationEquServiceImpl implements OperationEquService {

    @Resource
    private OperationEquMapper operationEquMapper;

    @Override
    public Long createOperationEqu(OperationEquSaveReqVO createReqVO) {
        // 插入
        OperationEquDO operationEqu = BeanUtils.toBean(createReqVO, OperationEquDO.class);
        operationEquMapper.insert(operationEqu);
        // 返回
        return operationEqu.getId();
    }

    @Override
    public void updateOperationEqu(OperationEquSaveReqVO updateReqVO) {
        // 校验存在
        validateOperationEquExists(updateReqVO.getId());
        // 更新
        OperationEquDO updateObj = BeanUtils.toBean(updateReqVO, OperationEquDO.class);
        operationEquMapper.updateById(updateObj);
    }

    @Override
    public void deleteOperationEqu(Long id) {
        // 校验存在
        validateOperationEquExists(id);
        // 删除
        operationEquMapper.deleteById(id);
    }

    private void validateOperationEquExists(Long id) {
        if (operationEquMapper.selectById(id) == null) {
            throw exception(OPERATION_EQU_NOT_EXISTS);
        }
    }

    @Override
    public OperationEquDO getOperationEqu(Long id) {
        return operationEquMapper.selectById(id);
    }

    @Override
    public PageResult<OperationEquDO> getOperationEquPage(OperationEquPageReqVO pageReqVO) {
        return operationEquMapper.selectPage(pageReqVO);
    }

}